import axios from "axios";

/**
 * axios的二次封装
 */
class Http {
    constructor() {
        this.instance = axios.create({
            baseURL: "http://localhost:8080/api",
            timeout: 20000
        })

        //添加请求拦截器
        this.instance.interceptors.request.use((config) => {
            const token = localStorage.getItem('token')
            if (token) {
                config.headers.Authorization = token
            }
            return config
        })

        // 添加响应拦截器
        this.instance.interceptors.response.use((response) => {
            if (response.status == 200 && response.data.code == 200) {
                return response.data
            } else {
                return response.data
            }
        })
    }

    /**
     * @description
     * @param {string} url 
     * @param {object} data 
     * @param {object} config 
     */
    get(url, data = {}, config = {}) {
        return this.instance.get(url, {
            params: data,
            ...config
        })
    }

    /**
     * @description
     * @param {string} url 
     * @param {object} data 
     * @param {object} config 
     */
    post(url, data = {}, config = {}) {
        return this.instance.post(url, data, config)
    }

    /**
     * @description
     * @param {string} url 
     * @param {object} data 
     * @param {object} config 
     */
    delete(url, data = {}, config = {}) {
        return this.instance.delete(url, {
            params: data,
            ...config
        })
    }
    /**
        * @description
        * @param {string} url 
        * @param {object} data 
        * @param {object} config 
        */
    patch(url, data = {}, config = {}) {
        return this.instance.patch(url, data, config)
    }





}

const request = new Http()

export default request